package com.lagou.config;

import com.lagou.pojo.Configuration;
import com.lagou.pojo.MappedStatement;
import com.lagou.pojo.SqlType;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.List;
import java.util.Locale;

public class XMLMapperBuilder {
    private Configuration configuration;

    public XMLMapperBuilder(Configuration configuration) {
        this.configuration = configuration;
    }


    public void parse(InputStream inputStream) throws DocumentException {
        Document document = new SAXReader().read(inputStream);
        Element rootElement = document.getRootElement();

        String namespace = rootElement.attributeValue("namespace");

        SqlType[] sqlTypes = SqlType.values();
        for (SqlType sqlType : sqlTypes) {
            List<Element> nodes = rootElement.selectNodes(
                    String.format(Locale.ROOT, "//%s", sqlType.getValue()));
            if ((nodes == null) || nodes.isEmpty()) {
                continue;
            }

            parseNodes(nodes, namespace, sqlType);
        }
    }

    /**
     * 解析表示数据库语句的元素
     *
     * @param nodes 表示数据库语句的元素的列表
     * @param namespace 元素对应的命名空间
     * @param sqlType 数据库语句的类型
     */
    private void parseNodes(List<Element> nodes, String namespace, SqlType sqlType) {
        for (Element element : nodes) {
            String id = element.attributeValue("id");
            String resultType = element.attributeValue("resultType");
            String paramterType = element.attributeValue("paramterType");
            String sqlText = element.getTextTrim();

            MappedStatement mappedStatement = new MappedStatement();
            mappedStatement.setId(id);
            mappedStatement.setResultType(resultType);
            mappedStatement.setParamterType(paramterType);
            mappedStatement.setSql(sqlText);
            mappedStatement.setSqlType(sqlType);

            String key = namespace+"."+id;
            configuration.getMappedStatementMap().put(key, mappedStatement);

        }
    }


   /* public void parse(InputStream inputStream) throws DocumentException {
        Document document = new SAXReader().read(inputStream);
        Element rootElement = document.getRootElement();
        String namespace = rootElement.attributeValue("namespace");

        List<Element> list = rootElement.selectNodes("//select");

        for (Element element: list){
            String id = element.attributeValue("id");
            String resultType = element.attributeValue("resultType");
            String paramterType = element.attributeValue("paramterType");
            String sqlText = element.getTextTrim();

            MappedStatement mappedStatement = new MappedStatement();
            mappedStatement.setId(id);
            mappedStatement.setParamterType(paramterType);
            mappedStatement.setResultType(resultType);
            mappedStatement.setSql(sqlText);

            String key = namespace + "." + id;
            configuration.getMappedStatementMap().put(key, mappedStatement);
        }
    }*/



}
